package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private  CheckItemService checkItemService;


    //  add   update
    @Override
    public boolean saveUpdate(CheckGroupDTO checkGroupDTO) {
        //   添加 （insert  中间  for  insert）和 编辑 (update  中间表 根据当前检查组id 清空  再 for  insert)
        Integer gid = checkGroupDTO.getId();
        boolean flag = true;
        if(gid==null){
            //  insert
             flag = save(checkGroupDTO);//  将主键 注入  checkGroupDTO
            gid = checkGroupDTO.getId();
        }else{
            //  update
           flag =  saveOrUpdate(checkGroupDTO);
            //  根据检查组id  删除中间表对应检查项信息
            baseMapper.deleteCheckItemIdsByGroupId(gid);
        }
        //  中间表录入  for
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
         if(checkitemIds!=null){
             for (Integer checkitemId : checkitemIds) {
                 baseMapper.addGroupIdAndCheckItemId(gid,checkitemId);
             }
         }
        return flag;
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageData =null;
        //  条件构造器
        QueryWrapper<CheckGroup> wrapper = new QueryWrapper<CheckGroup>();
        wrapper.eq("is_delete",0);
        //  1  mp  分页查询   完成分页查询   select  *  from   xxx  limit     count(1)
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            //  无条件分页
            //  page  结合 分页插件
            pageData   = page(new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else{
            wrapper.and(i->i.like("name",queryPageBean.getQueryString()).or().like("helpcode",queryPageBean.getQueryString()));
            pageData   = page(new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }
        // 2  结果集封装 PageResult返回
        PageResult pageResult = new PageResult(pageData.getTotal(),pageData.getRecords());
        return pageResult;
    }

    @Override
    public CheckGroupVO findCheckIteminfosByGroupId(int id) {
        //  CheckGroupVO(检查组基本信息，对应检查项ids, 所有检查项列表)
        CheckGroupVO checkGroupVO =  baseMapper.findCheckGroupById(id);
        checkGroupVO.setCheckItems(checkItemService.list());
        int[] checkItemIds = baseMapper.findCheckItemIdsByGroupId(id);
        checkGroupVO.setCheckItemIds(checkItemIds);//  中间表  根据检查组id 查询对应检查项ids
        return checkGroupVO;
    }
}
